package client.comm.baoding.ui

import android.content.ClipData
import android.content.ClipboardManager
import android.graphics.Bitmap
import android.graphics.Canvas
import android.provider.MediaStore
import android.text.TextUtils
import android.widget.LinearLayout
import androidx.lifecycle.viewModelScope
import client.comm.commlib.toast.toast
import client.comm.commlib.utils.StoreUtil
import client.comm.commlib.utils.Util
import client.comm.baoding.R
import client.comm.baoding.api.bean.UserInfo
import client.comm.baoding.databinding.ActivityTuiguangBinding
import client.comm.baoding.ui.vm.TuiGuangViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import client.comm.baoding.BaseMiddleActivity

class TuiGuangActivity : BaseMiddleActivity<TuiGuangViewModel, ActivityTuiguangBinding>() {

    val user: UserInfo? by lazy {
        StoreUtil.getBean<UserInfo>(this@TuiGuangActivity)
    }

    override fun getLayout(): Int {
        return R.layout.activity_tuiguang
    }

    override fun initViewModel(): TuiGuangViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
    }

    override fun initData() {

        viewModel.viewModelScope.launch {
            val bitmap: Bitmap? = withContext(Dispatchers.IO) {
                var bitmap: Bitmap? = null
                if (user != null && !TextUtils.isEmpty(user!!.invite)) {
                    try {
                        bitmap = Util.createQRCode(user!!.invite, 400, 400)
                    } catch (e: java.lang.Exception) {
                    }
                }
                bitmap
            }

            bitmap?.let {
                withContext(Dispatchers.Main) {
                    val inviteCode = user!!.invite.substring(user!!.invite.indexOf("=") + 1)
                    binding.tgm = "推广码：${inviteCode}"
                    binding.codeIv.setImageBitmap(bitmap)
                    binding.codeIv.postDelayed({
                        val width = binding.codeIv.width
                        val parentHeight = binding.drawWrap.height
                        val top = (parentHeight * 2f / 3.8f - width / 2f).toInt()
                        val params = binding.codeIv.layoutParams as LinearLayout.LayoutParams
                        params.topMargin = top
                        params.leftMargin = dipToPx(resources!!, -4f).toInt()
                        binding.codeIv.layoutParams = params
                    }, 50)
                }
            }
        }
    }

    fun fzlj() {
        user?.invite?.let {
            if (!it.isNullOrEmpty()) {
                val cmb = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
                cmb.setPrimaryClip(ClipData.newPlainText(null, it))
                toast("推广链接已复制")
            } else {
                toast("推广链接为空复制失败")
            }
        }
    }

    fun save() {
        viewModel.viewModelScope.launch {
            viewModel.isShowing.value = true
            val r = withContext(Dispatchers.IO) {
                var ret = false
                try {
                    val bitmap = Bitmap.createBitmap(
                        binding.drawWrap.width, binding.drawWrap.height,
                        Bitmap.Config.ARGB_8888
                    )
                    val canvas = Canvas(bitmap)
                    //                            canvas.drawColor(Color.parseColor("#180f48"));
                    binding.drawWrap.draw(canvas)
                    val path = MediaStore.Images.Media.insertImage(
                        contentResolver,
                        bitmap,
                        "邀请",
                        "description"
                    )

                    ret = true
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                ret
            }

            if (r) {
                toast("已保存")
            } else {
                toast("保存失败")
            }
            viewModel.isShowing.value = false

        }
    }
}